<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2020/3/1
 * Time: 16:06
 */

namespace app\common\model;


use think\Model;
use think\model\concern\SoftDelete;

class User extends Model
{
    use SoftDelete;
    /**
     * 关联用户信息
     * @return \think\model\relation\HasOne
     */
    public function info()
    {
        return $this->hasOne('Userinfo','user_id','id');
    }

    /**
     * 注册添加
     * @param int $type 1：微信注册 0：手机注册
     * @param string $phone 号码
     * @param string $password 密码
     * @param string $openid
     * @return array
     */
    public static function add($type='',$phone='',$password='',$openid='')
    {
        $data = [];

        $validate = new \app\common\validate\User();

        if($type===0){

            $data = [
                'phone'=>$phone,
                'password'=>$password
            ];

            if(!$validate->scene('phone')->check($data)){
                return ['code'=>1,'error'=>$validate->getError()];
            }
        }

        if($type===1){

            $data = ['openid'=>$openid];

            if(!$validate->scene('wx')->check($data))
            {
                return ['code'=>1,'error'=>$validate->getError()];
            }
        }

        if(empty($data)){
            return ['code'=>1,'error'=>'注册类型错误'];
        }

        $user = self::create($data);

        if($user->id){
            return ['code'=>1,'error'=>'未知错误'];
        }

        return ['code'=>0,'msg'=>'注册成功'];
    }

    /**
     * 更改状态
     * @param int $id 用户ID
     * @param string $status 状态 0:女 1:男
     * @return array
     */
    public static function status($id,$status="0")
    {

        $data = [
            'id'=>$id,
            'status'=>$status
        ];


        self::update($data);

        return ['code'=>0,'msg'=>'状态更改成功'];
    }
}